package com.digitalchina.lxnsy.viewholder;

import androidx.annotation.NonNull;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.ShareOrderVO;
import com.digitalchina.lxnsy.interfaces.IOrderCallBack;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.ToastUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2017年7月27日 19:54
 * 修改备注：
 * ================================================
 */

public class ShareSellOrderItemHolder extends BaseViewHolder<ShareOrderVO> {

    // 商家logo
    private SimpleDraweeView sdvLogo;
    // 商家名称
    private TextView tvFarmName;
    // 商家电话
    private TextView tvTel;
    // 订单状态
    private TextView tvStatus;
    // 商品容器
    private LinearLayout layout;
    // 商品数量
    private TextView tvNum;
    // 商品总价
    private TextView tvPrice;
    // 邮费
    private TextView tvFreight;
    // 左边按钮
    private TextView tvLeft;
    // 右边按钮
    private TextView tvRight;

    private IOrderCallBack iOrderCallBack;


    public ShareSellOrderItemHolder(ViewGroup parent, IOrderCallBack iOrderCallBack) {
        super(parent, R.layout.item_my_share_buy_order);
        this.iOrderCallBack = iOrderCallBack;
        sdvLogo = $(R.id.item_my_order_iv_farm_logo);
        tvFarmName = $(R.id.item_my_order_tv_farm_name);
        tvTel = $(R.id.item_my_order_tv_farm_tel);
        tvStatus = $(R.id.item_my_order_tv_order_status);
        layout = $(R.id.item_my_order_linearLayout);
        tvNum = $(R.id.item_my_order_tv_product_number);
        tvPrice = $(R.id.item_my_order_tv_order_price);
        tvFreight = $(R.id.item_my_order_tv_order_freight);
        tvLeft = $(R.id.item_my_order_tv_button_left);
        tvRight = $(R.id.item_my_order_tv_button_right);

    }

    @Override
    public void setData(final ShareOrderVO data) {
        super.setData(data);

        FrescoUtil.showImageSmall(data.getSellerLogo(), sdvLogo);

        tvFarmName.setText(data.getSellerName());
//        if (StringUtil.isStrEmpty(data.getSellerPhone())) {
//            tvTel.setVisibility(View.GONE);
//        } else {
//            tvTel.setVisibility(View.VISIBLE);
//            tvTel.setText(data.getSellerPhone());
//        }

        tvStatus.setText(data.getEqualStatusDesc());
        tvNum.setText(data.getOrderItems() != null ? data.getOrderItems().size() + "" : "1");
        tvPrice.setText(data.getTotalPriceReal() + "");
        tvFreight.setText("（含运费" + data.getFreightPriceOrigin() + "元）");


        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        layout.removeAllViews();
        for (int i = 0; i < data.getOrderItems().size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.view_my_order_product, null);
            SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.view_my_order_product_iv_picture);
            TextView tvTitle = (TextView) view.findViewById(R.id.view_my_order_product_tv_title);
            TextView tvPrice = (TextView) view.findViewById(R.id.view_my_order_product_tv_price);
            TextView tvNumber = (TextView) view.findViewById(R.id.view_my_order_product_tv_number);
            TextView tvAttrName = (TextView) view.findViewById(R.id.view_my_order_product_tv_attrName);

            FrescoUtil.showImageSmall(data.getOrderItems().get(i).getProductImg(), simpleDraweeView);
            tvTitle.setText(data.getOrderItems().get(i).getProductName());
            tvPrice.setText(data.getOrderItems().get(i).getPrice() + "");
            tvNumber.setText(String.valueOf(data.getOrderItems().get(i).getQuantity()));
            if (StringUtil.isStrEmpty(data.getOrderItems().get(i).getAttrName())) {
                tvAttrName.setVisibility(View.INVISIBLE);
            } else {
                tvAttrName.setVisibility(View.VISIBLE);
                tvAttrName.setText(data.getOrderItems().get(i).getAttrName());
            }


            layout.addView(view);
        }

        switch (data.getEqualStatus()) {
            case 10://待提货（已支付 只有自提）
                setButtonText("", "验证提货码");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new MaterialDialog.Builder(getContext())
                                .title("提示")
                                .content("验证提货码")
                                .inputType(InputType.TYPE_CLASS_NUMBER)
                                .input("请输入提货码", "", new MaterialDialog.InputCallback() {

                                    @Override
                                    public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                                    }
                                })
                                .positiveText("确定")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        assert dialog.getInputEditText() != null;
                                        iOrderCallBack.onVerificationClick(getAdapterPosition(), dialog.getInputEditText().getText().toString());
                                    }
                                })
                                .show();

                    }
                });
                break;
            case 20://待发货（已支付 只有邮寄）
                setButtonText("", "确认发货");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 使用确认收货方法
                        iOrderCallBack.onAffirmClick(getAdapterPosition());
                    }
                });
                break;
            case 19://待支付 自提
                setButtonText("", "查看订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
            case 29://待支付 邮寄
                setButtonText("", "查看订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
            case 21://待收货
                setButtonText("", "查看物流");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onLogisticsClick(getAdapterPosition());
                    }
                });
                break;
            case 11://待评价 自提
                setButtonText("", "查看订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
            case 22://待评价 邮寄
                setButtonText("", "查看物流");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
            case 13://已取消 自提
                setButtonText("", "查看订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
            case 24://已取消 邮寄
                setButtonText("", "查看订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
            case 12://已关闭 自提
                setButtonText("", "查看订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
            case 23://已关闭 邮寄
                setButtonText("", "查看订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onDetailClick(getAdapterPosition());
                    }
                });
                break;
        }

    }

    /**
     * 删除订单
     */
    private void deleteOrder(final int pos) {
        new MaterialDialog.Builder(getContext())
                .title("提示")
                .content("是否确认删除该订单？")
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        iOrderCallBack.onDeleteClick(pos);

                    }
                })
                .show();
    }

    /**
     * 取消订单
     */
    private void cancelOrder(final int pos) {
        new MaterialDialog.Builder(getContext())
                .title("提示")
                .content("您确定要取消订单吗？")
                .items(R.array.cancelReason)
                .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        if (text == null || text == "") {
                            ToastUtil.showToast(getContext(), "请选择一个理由！");
                            return true;
                        }
                        iOrderCallBack.onCancelClick(pos, text.toString());
                        return true;
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .show();
    }

    private void setButtonText(String leftButtonText, String rightButtonText) {
        // 左边按钮
        if (StringUtil.isStrEmpty(leftButtonText))
            tvLeft.setVisibility(View.GONE);
        else {
            tvLeft.setText(leftButtonText);
            tvLeft.setVisibility(View.VISIBLE);
        }

        // 右边按钮
        if (StringUtil.isStrEmpty(rightButtonText))
            tvRight.setVisibility(View.GONE);
        else {
            tvRight.setText(rightButtonText);
            tvRight.setVisibility(View.VISIBLE);
        }
    }

}
